package one.bugu.android.demon.ui.fragment.snatch;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.MessageParentBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseFragment;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.adapter.MessageAdapter;
import one.bugu.android.demon.ui.refresh.SmartRefreshLayout;
import one.bugu.android.demon.ui.refresh.listener.OnRefreshLoadmoreListener;
import one.bugu.android.demon.ui.refresh.listener.RefreshLayout;
import one.bugu.android.demon.util.PreferencesUtil;

/**
 * 消息页面
 */
@LKContentView(R.layout.fragment_message)
public class MessageFragment extends MyBaseFragment {

    @LKInjectView(R.id.listview)
    private ListView listView;

    @LKInjectView(R.id.textview)
    private TextView textView;

    @LKInjectView(R.id.ref_layout)
    private SmartRefreshLayout ref_layout;

    private static final String PAGE_TYPE = "pageType";
    private int pageType = WARN_MSG;

    private int limit = 10;

    private int offset = 1;

    public static final int WARN_MSG = 0;
    public static final int SYS_MSG = 1;

    private String token;

    private MessageAdapter messageAdapter;

    public MessageFragment() {

    }

    /**
     * Fragment初始化
     *
     * @param param1
     * @return
     */
    public static MessageFragment newInstance(int param1) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putInt(PAGE_TYPE, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        super.initView();
        if (getArguments() != null) {
            pageType = getArguments().getInt(PAGE_TYPE);
        }
        messageAdapter = new MessageAdapter(getContext(), pageType);
        listView.setAdapter(messageAdapter);

        ref_layout.setEnableRefresh(true);//启用刷新
        ref_layout.setEnableLoadmore(true);//启用加载
    }

    @Override
    protected void initData() {
        super.initData();
        token = PreferencesUtil.getInstance().getString(getContext(), Constant.TOKEN);
        offset = 1;
        getNetData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ref_layout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //刷新数据
                offset++;
                getNetData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                offset = 1;
                getNetData();
            }
        });
    }

    private void getData(int msgType) {
        token = PreferencesUtil.getInstance().getString(getContext(), Constant.TOKEN);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("msgType", String.valueOf(msgType));
        map.put("limit", String.valueOf(limit));
        map.put("offset", String.valueOf(offset));
        map.put("order", "desc");
        map.put("sort", "createTime");
        LKUtil.getHttpManager().postBody(HttpConstant.MESSAGE_URL, map, handler);
    }

    /**
     * 获取信息详情网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<MessageParentBean> handler = new BaseHttpAsycResponceHandler<MessageParentBean>(true) {
        @Override
        public void onSuccess(MessageParentBean bgtInfoBeans) {
            super.onSuccess(bgtInfoBeans);
            ref_layout.finishRefresh();
            ref_layout.finishLoadmore();
            if (bgtInfoBeans != null && !bgtInfoBeans.getList().isEmpty()) {
                //保存消息条数
                PreferencesUtil.getInstance().putInt(getContext(), Constant.MSG_NUM, bgtInfoBeans.getNum());
                if (offset == 1) {
                    //刷新数据
                    messageAdapter.updateData(bgtInfoBeans.getList());
                } else {
                    //加载数据
                    messageAdapter.addData(bgtInfoBeans.getList());
                }
                //是否去加载下一页
                ref_layout.setEnableLoadmore(!(bgtInfoBeans.getList().size() < limit));
            } else {
                ref_layout.setEnableLoadmore(false);
            }

            //返回数据为空时 显示无消息的文字
            if (messageAdapter == null || messageAdapter.getCount() <= 0) {
                textView.setVisibility(View.VISIBLE);
                ref_layout.setVisibility(View.GONE);
            } else {
                textView.setVisibility(View.GONE);
                ref_layout.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            ref_layout.finishRefresh();
            ref_layout.finishLoadmore();
        }

        @Override
        public void onError() {
            super.onError();
            ref_layout.finishRefresh();
            ref_layout.finishLoadmore();
        }
    };

    /**
     * 获取数据
     */
    private void getNetData() {
        //获取数据
        if (pageType == WARN_MSG) {
            getData(10);
        } else {
            getData(30);
        }
    }
}
